/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * InsuranceDialog.java
 *
 * Created on Dec 11, 2011, 2:03:08 PM
 */

package healthcare;

import java.util.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.table.*; 

/**
 *
 * @author Jonathan A. Earley
 */
public class InsuranceDialog extends javax.swing.JDialog {

    /** Creates new form InsuranceDialog */
    public InsuranceDialog(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
    }

    public InsuranceDialog() {
          java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                InsuranceDialog dialog = new InsuranceDialog(new javax.swing.JFrame(), true);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {
                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        ClientSSTextField = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        CaseworkerIDTextField = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        InsuranceNumberTextField = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        PrimaryInsuraceTextField = new javax.swing.JTextField();
        jLabel5 = new javax.swing.JLabel();
        HourlyRateTextField = new javax.swing.JTextField();
        jLabel6 = new javax.swing.JLabel();
        SaveButton = new javax.swing.JButton();
        DoneButton = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        jLabel1.setText("Client SSN:");

        ClientSSTextField.setEnabled(false);
        ClientSSTextField.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                ClientSSTextFieldActionPerformed(evt);
            }
        });

        jLabel2.setText("Caseworker ID:");

        jLabel3.setText("Invoice Number:");

        jLabel4.setText("Primary Insurance:");

        jLabel5.setText("Hourly Billable Rate:");

        jLabel6.setFont(new java.awt.Font("Tahoma", 1, 18));
        jLabel6.setText("Insured");

        SaveButton.setText("Save");
        SaveButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                SaveButtonActionPerformed(evt);
            }
        });

        DoneButton.setText("Done");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(31, 31, 31)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jLabel3)
                            .addComponent(jLabel4)
                            .addComponent(jLabel2)
                            .addComponent(jLabel1)
                            .addComponent(jLabel5))
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                .addComponent(ClientSSTextField, javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(CaseworkerIDTextField, javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(InsuranceNumberTextField, javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(PrimaryInsuraceTextField, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, 106, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGroup(layout.createSequentialGroup()
                                    .addComponent(SaveButton)
                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                    .addComponent(DoneButton)))
                            .addComponent(HourlyRateTextField, javax.swing.GroupLayout.PREFERRED_SIZE, 82, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addComponent(jLabel6, javax.swing.GroupLayout.PREFERRED_SIZE, 85, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(135, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel6)
                        .addGap(51, 51, 51)
                        .addComponent(jLabel1))
                    .addComponent(ClientSSTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel2, javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(CaseworkerIDTextField, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(InsuranceNumberTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel3))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(PrimaryInsuraceTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel4))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(HourlyRateTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel5))
                .addGap(28, 28, 28)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(DoneButton)
                    .addComponent(SaveButton))
                .addContainerGap(54, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void ClientSSTextFieldActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_ClientSSTextFieldActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_ClientSSTextFieldActionPerformed

    private void SaveButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_SaveButtonActionPerformed
        // TODO add your handling code here:
           String clientSsn = ClientSSTextField.getText();
    
    if (clientSsn == null || clientSsn.length() == 0) {
        JOptionPane.showMessageDialog(this, "SSN is required", "Notice",  JOptionPane.PLAIN_MESSAGE);
    } else {
        ClientSSTextField.setEditable(false);  //prevent editing of SSN in form

        //package form data into Client object
        Insured insured = new Insured();
        insured.CLIENTSSN	=  ClientSSTextField.getText();	
        insured.CASEWORKERID	=  CaseworkerIDTextField.getText();	
        insured.INSURANCENUMBER	=  InsuranceNumberTextField.getText();	
        insured.ISPRIMARY       =  PrimaryInsuraceTextField.getText();	
        insured.HOURLYBILLRATE  =  HourlyRateTextField.getText();	

        try {
            if (Database.selectInsured(clientSsn) == null) {  //does record exist in DB?
                Database.insertInsured(insured);
                JOptionPane.showMessageDialog(this, "Insurance added to database", "Notice",  JOptionPane.PLAIN_MESSAGE);
            } else {
                Database.updateInsured(insured);
                JOptionPane.showMessageDialog(this, "Insurance updated in database", "Notice",  JOptionPane.PLAIN_MESSAGE);
            }
        } catch (AppException ae) {
            JOptionPane.showMessageDialog(this, ae.toString(), "Notice",  JOptionPane.PLAIN_MESSAGE);
        }
    }
    }//GEN-LAST:event_SaveButtonActionPerformed

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                InsuranceDialog dialog = new InsuranceDialog(new javax.swing.JFrame(), true);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {
                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTextField CaseworkerIDTextField;
    private javax.swing.JTextField ClientSSTextField;
    private javax.swing.JButton DoneButton;
    private javax.swing.JTextField HourlyRateTextField;
    private javax.swing.JTextField InsuranceNumberTextField;
    private javax.swing.JTextField PrimaryInsuraceTextField;
    private javax.swing.JButton SaveButton;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    // End of variables declaration//GEN-END:variables

}
